package com.yuheadline.user.controller;

import com.dust.yuheadline.common.utils.Result;
import com.yuheadline.model.user.dto.UserLoginDto;
import com.yuheadline.model.user.dto.UserRegisterDto;
import com.yuheadline.model.user.vo.GetUserInfoVo;
import com.yuheadline.model.user.vo.UserLoginVo;
import com.yuheadline.model.user.vo.UserRegisterVo;
import com.yuheadline.user.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;

/**
 * @author 高翔宇
 * @since 2024/4/29 周一 下午10:25
 */
@RestController
@RequestMapping("/api/user")
@RequiredArgsConstructor
@Slf4j
@Tag(name = "用户管理接口")
public class UserController {
    private final UserService userService;

    /**
     * 登录
     */
    @Operation(summary = "用户登录")
    @PostMapping("/login")
    public Result<UserLoginVo> login(@Validated @RequestBody UserLoginDto userLoginDto) {
        log.info("用户登录：{}", userLoginDto);
        return userService.login(userLoginDto);
    }

    /**
     * 注册
     */
    @Operation(summary = "用户注册")
    @PostMapping("/register")
    public Result<UserRegisterVo> register(@Validated @RequestBody UserRegisterDto userRegisterDto) {
        log.info("用户注册：{}", userRegisterDto);
        return userService.register(userRegisterDto);
    }

    /**
     * 查询当前用户基本信息
     */
    @Operation(summary = "查询当前用户基本信息")
    @GetMapping("/info")
    public Result<GetUserInfoVo> queryUserInfo() {
        log.info("查询当前用户基本信息");
        return userService.queryUserInfo();
    }

    /**
     * 更新头像
     */
    @Operation(summary = "更新头像")
    @PostMapping(value = "/avatar", headers = "content-type=multipart/form-data")
    public Result<String> updateAvatar(@RequestParam("avatar") MultipartFile multipartFile) {
        log.info("上传头像...");
        return userService.updateAvatar(multipartFile);
    }

    /**
     * 更新用户名
     */
    @Operation(summary = "更新用户信息", description = "更新允许用户自行管理的信息")
    @PutMapping("/username")
    public Result<?> updateUsername(String username) {
        log.info("更新用户名：{}", username);
        return userService.updateUsername(username);
    }

    /**
     * 更新自我介绍
     */
    @Operation(summary = "更新自我介绍")
    @PutMapping("/introduction")
    public Result<?> updateIntroduction(@RequestBody HashMap<String, String> introduction) {
        String introductionStr = introduction.get("introduction");
        log.info("更新自我介绍：{}", introductionStr);
        return userService.updateIntroduction(introductionStr);
    }
}
